⚡️ Speed up function extract_internal_format
by 31%
#62
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
📄 31% (0.31x) speedup for
extract_internal_format
inguardrails/schema/rail_schema.py
⏱️ Runtime :
926 microseconds
→705 microseconds
(best of106
runs)📝 Explanation and details
The optimized code achieves a 31% speedup through two key string processing optimizations:
1. Replaced
split()
withpartition()
format.split("; ")
andinternal.split(": ")
create lists and process all occurrencesformat.partition("; ")
andinternal.partition(": ")
stop at the first delimiter, returning exactly 3 elements2. Eliminated redundant string operations
"; ".join(custom_rest)
reconstructs the custom format string from a listpartition()
RailTypes.get()
call by reusing the cached result3. Avoided redundant dictionary lookups
RailTypes.get(internal_type)
twice (once for the check, once for assignment)rail_type
variable and reuses itThe optimizations are particularly effective for large-scale test cases where the performance gains are most dramatic (up to 400% faster). For inputs with many custom formats or long strings, avoiding the overhead of list creation, joining operations, and redundant dictionary lookups provides substantial benefits. The improvements are consistent across all input types and sizes, with smaller gains on simple inputs (17-25%) and massive gains on complex inputs with many semicolon-separated segments.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
⏪ Replay Tests and Runtime
test_pytest_testsunit_teststest_guard_log_py_testsintegration_teststest_guard_py_testsunit_testsvalidator__replay_test_0.py::test_guardrails_schema_rail_schema_extract_internal_format
To edit these changes
git checkout codeflash/optimize-extract_internal_format-mh1t26ud
and push.